#region Copyright
// Copyright (c) 2010, Kazi Manzur Rashid <kazimanzurrashid@gmail.com>.
// This source is subject to the Microsoft Public License. 
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. 
// All other rights reserved.
#endregion

namespace LiveWriter.Plugins.HighlightJS
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Xml;
    using System.Xml.Linq;

    internal static class SupportedSettings
    {
        public static readonly IList<Pair> Languages = LoadLanguages();

        private static IList<Pair> LoadLanguages()
        {
            string languageFile = Path.Combine(Path.Combine(RuntimeInfo.PluginPath, "HighlightJS"), "languages.xml");

            XDocument doc;

            if (File.Exists(languageFile))
            {
                doc = XDocument.Load(languageFile);
            }
            else
            {
                Stream resource = typeof(SupportedSettings).Assembly.GetManifestResourceStream("LiveWriter.Plugins.HighlightJS.languages.xml");
                doc = XDocument.Load(XmlReader.Create(resource));
            }

            IList<Pair> languages = doc.Root
                                       .Elements("language")
                                       .Select(x => new Pair { Key = x.Attribute("internalName").Value, Value = x.Attribute("displayName").Value })
                                       .OrderBy(lang => lang.Key)
                                       .ToList();

            return languages;
        }
    }
}